package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ThanhVienDao;

import model.ThanhVienModel;

public class ThanhVienController extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req,HttpServletResponse res){
		try {
			String actionType = req.getParameter("actionType");
			String pageNext= "";
			if (actionType.equals("AddNew")){
				String tenDangNhap = req.getParameter("tenDN");
				String matKhau = req.getParameter("matKhau");
				String vaiTro = req.getParameter("vaiTro");
				String ghiChu = req.getParameter("ghiChu");
				
				ThanhVienModel model = new ThanhVienModel();
				model.setTendn(tenDangNhap);
				model.setMatkhau(matKhau);
				model.setVaitro(vaiTro);
				model.setGhichu(ghiChu);
				
				Boolean insertStatus = ThanhVienDao.insertThanhVien(model);
				
				
				if(insertStatus){
					pageNext="homePage.jsp";
				}else{
					req.setAttribute("Lỗi", "Vai trò không được là chữ");
					req.setAttribute("ValueObject", model);
					pageNext="addUser.jsp";
				}
				
				RequestDispatcher rd = req.getRequestDispatcher(pageNext);
				rd.forward(req, res);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse res){
		doPost(req, res);
	}
}
